package com.cwj.sqlgenerator;

import org.junit.jupiter.api.Test;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
 * @author wujie.chen
 * 2022/9/23 15:36
 */
public class SqlConsole {

    @Test
    public void t_taimei_tenant_id_mappingSQL(){
        System.out.println("start...");
        String data = "/com/cwj/sqlgenerator/tenant.csv";
        InputStream is = SqlConsole.class.getResourceAsStream(data);
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.isEmpty()) {
                    continue;
                }
                String[] tokens = StringUtils.tokenizeToStringArray(line, ",");

                String id = UUID.randomUUID().toString().replaceAll("-", "");
                String tenantId = tokens[0];
                String taimeiTenantId = tokens[0];

                String sql = String.format(
                    "INSERT INTO `t_taimei_tenant_id_mapping` (`id`,`tenant_id`,`taimei_tenant_id`)VALUES('%s','%s','%s');",
                    id, tenantId, taimeiTenantId);
                System.out.println(sql);
            }
        }
        catch (IOException ex) {
            throw new IllegalStateException("Could not load '" + data + "'", ex);
        }

    }

    @Test
    public void t_taimei_department_id_mappingSQL(){
        System.out.println("start...");
        String data = "/com/cwj/sqlgenerator/department.csv";
        InputStream is = SqlConsole.class.getResourceAsStream(data);
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.isEmpty()) {
                    continue;
                }
                String[] tokens = StringUtils.tokenizeToStringArray(line, ",");

                String id;
                String tenantId = tokens[0];
                String taimeiDepartmentId = tokens[1];
                String departmentId = tokens.length == 3 ? tokens[2] : null;
                if(departmentId == null){
                    id = UUID.randomUUID().toString().replaceAll("-", "");
                    String sql = String.format(
                        "INSERT INTO `t_department` (`id`, `tenant_id`, `dept_no`, `dept_name`, `established`, `staff_number`, `seq`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s');",
                        id, tenantId, "001", "顶级部门","2022-10-10 18:00:00", "0", "0");
                    departmentId = id;
                    System.out.println(sql);

                    String treeId = UUID.randomUUID().toString().replaceAll("-", "");
                    String sql2 = String.format(
                        "INSERT INTO `t_dept_tree_path` (`id`, `dept_id`, `tree_id`, `tree_path`, `seq`) VALUES ('%s', '%s', '%s', '%s', '%s');",
                        treeId, departmentId, treeId,"/" + departmentId, "0");
                    System.out.println(sql2);
                }

                id = UUID.randomUUID().toString().replaceAll("-", "");
                String sql = String.format(
                    "INSERT INTO `t_taimei_department_id_mapping` (`id`, `dept_id`, `taimei_dept_id`) VALUES ('%s','%s','%s');",
                    id, departmentId, taimeiDepartmentId);
                System.out.println(sql);
            }
        }
        catch (IOException ex) {
            throw new IllegalStateException("Could not load '" + data + "'", ex);
        }

    }

    @Test
    public void dept_treeSQL(){
        System.out.println("start...");
        String data = "/com/cwj/sqlgenerator/dept_tree.csv";
        InputStream is = SqlConsole.class.getResourceAsStream(data);
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.isEmpty()) {
                    continue;
                }
                String[] tokens = StringUtils.tokenizeToStringArray(line, ",");
                String treeId = UUID.randomUUID().toString().replaceAll("-", "");
                String departmentId = tokens[0];
                String sql2 = String.format(
                    "INSERT INTO `t_dept_tree_path` (`id`, `dept_id`, `parent_id`, `tree_id`, `tree_path`, `seq`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s');",
                    treeId, departmentId, null, treeId,"/" + departmentId, "0");
                System.out.println(sql2);
            }
        }
        catch (IOException ex) {
            throw new IllegalStateException("Could not load '" + data + "'", ex);
        }

    }

    @Test
    public void t_taimei_account_id_mappingSQL(){
        System.out.println("start...");
        String data = "/com/cwj/sqlgenerator/account.csv";
        InputStream is = SqlConsole.class.getResourceAsStream(data);
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.isEmpty()) {
                    continue;
                }
                String[] tokens = StringUtils.tokenizeToStringArray(line, ",");

                String id = UUID.randomUUID().toString().replaceAll("-", "");;
                String accountId = tokens[0];
                String taimeiAccountId = tokens[1];
                String sql = String.format(
                    "INSERT INTO `t_taimei_account_id_mapping` (`id`,`account_id`,`taimei_account_id`) VALUES ('%s', '%s', '%s');",
                    id, accountId, taimeiAccountId);
                System.out.println(sql);
            }
        }
        catch (IOException ex) {
            throw new IllegalStateException("Could not load '" + data + "'", ex);
        }

    }

    @Test
    public void account_user_mapping_tempSQL(){
        System.out.println("start...");
        String data = "/com/cwj/sqlgenerator/account_user_mapping.csv";
        InputStream is = SqlConsole.class.getResourceAsStream(data);
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.isEmpty()) {
                    continue;
                }
                String[] tokens = StringUtils.tokenizeToStringArray(line, ",");
                String id = UUID.randomUUID().toString().replaceAll("-", "");
                String accountId = tokens[0];
                String tenantId = tokens[1];
                String userId = tokens[2];
                String sql = String.format(
                    "INSERT INTO `t_account_user_mapping_temp` (`id`, `account_id`,`user_id`,`tenant_id`) VALUES ('%s','%s', '%s', '%s');",
                    id, accountId, userId, tenantId);
                System.out.println(sql);
            }
        }
        catch (IOException ex) {
            throw new IllegalStateException("Could not load '" + data + "'", ex);
        }

    }
}
